package command

import (
	"errors"
	"fmt"
	"geek_ros/framework/service"
	"github.com/gookit/color"
	"github.com/spf13/cobra"
)

func SdkCommand() *cobra.Command {
	command := &cobra.Command{
		Use:   "sdk",
		Short: "geek_sdk安装与更新",
		Long: "geek_sdk安装与更新",
		Example: "安装geek_sdk：geekros sdk install\n更新geek_sdk：geekros sdk update",
		Args: func(cmd *cobra.Command, args []string) error {
			if len(args) != 1 {
				return errors.New(color.Red.Text("请选择操作类型[install|update]"))
			}
			return nil
		},
		Run: func(cmd *cobra.Command, args []string) {
			if args[0] != "install" && args[0] != "update" {
				fmt.Println(color.Red.Text("请选择操作类型 [pub|sub]"))
				return
			}else{
				switch args[0] {
				case "install":
					fmt.Println("[info][sdk]：开始安装部署geek_sdk")
					service.Shell("sudo", "wget", "https://cdn.robomentor.com/software/sdk/geekros_sdk.zip")
					service.Shell("sudo", "unzip", "-d", "/opt/geek_sdk/", "geekros_sdk.zip")
					service.Shell("sudo", "rm", "-rf", "geekros_sdk.zip")
				case "update":
					fmt.Println("[info][sdk]：开始更新geek_sdk")
					service.Shell("sudo", "wget", "https://cdn.robomentor.com/software/sdk/geekros_sdk.zip")
					service.Shell("sudo", "unzip", "-d", "/opt/geek_sdk/", "geekros_sdk.zip")
					service.Shell("sudo", "rm", "-rf", "geekros_sdk.zip")
				}
			}
		},
	}
	return command
}
